package br.com.fiap.trabalho.junit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.Test;

import br.com.fiap.trabalho.dao.ActorDAO;
import br.com.fiap.trabalho.entity.Actor;
import br.com.fiap.trabalho.entity.Movie;
import br.com.fiap.trabalho.entity.Studio;
import br.com.fiap.trabalho.factory.DaoFactory;

public class JDBCActorDAOTestMetric extends DAOtest{
	
	private static ActorDAO actorDAO = DaoFactory.getDaoFactory(DaoFactory.JDBC).getActorDAO();
	
	public void createEntity() {
	
		for(int i = 0; i < numberOfIteractions; i++){
		
			Calendar calendar = Calendar.getInstance();
			calendar.set(1990, Calendar.APRIL, 17);
			Date birthDate = calendar.getTime();
			String actorName = "Teste Inclusao ActorDAO"+i;
		
			Actor actor = new Actor();
			actor.setFullName(actorName);
			actor.setBirthDate(birthDate);
		
			actorDAO.createActor(actor);
		}
		
		System.out.println("Tempo para criar entidades Actor JDBC: ");
	}
	
	public void listEntity() {

		actorDAO.selectActors(numberOfIteractions);
		
		System.out.println("Tempo para Listar entidades Actor JDBC: ");
	}

	public void updateEntity() {
		
		List<Actor> actorsForUpdate = actorDAO.selectActors(numberOfIteractions);
		
		int i = 0;
		for(Actor actor : actorsForUpdate){
			
			actor.setFullName("Teste Update ActorDAO"+i);
			actor.setBirthDate(Calendar.getInstance().getTime());
			
			actorDAO.createActor(actor);
			i++;
		}
		
		System.out.println("Tempo para Update entidades Actor JDBC: ");
	}
	
	@Test
	public void getActorsByMovie() {
		
		List<Actor> actorsWithMovie = actorDAO.selectActors(numberOfIteractions);

		for(Actor actor : actorsWithMovie){
			
			if(actor.getMovies() != null && !actor.getMovies().isEmpty()){
				
				for(Movie movie : actor.getMovies()){
					
					 actorDAO.selectActorsByMovie(movie);
				}
			}
		}
		
		System.out.println("Tempo para Listar Atores by Movie JDBC: ");
	}
	
	@Test
	public void createActorsAndMovie() {
		
		for(int i = 0; i < numberOfIteractions; i++){
			
			Calendar calendar = Calendar.getInstance();
			calendar.set(1990, Calendar.APRIL, 17);
			Date birthDate = calendar.getTime();
			String actorName = "Teste Inclusao ActorDAO"+i;
		
			Actor actor = new Actor();
			actor.setFullName(actorName);
			actor.setBirthDate(birthDate);
		
			List<Movie> movies = new ArrayList<Movie>();
			
			for(int j = 0; j < numberOfSecondLevelEntity; j++){
				
				Movie movie = new Movie();
				movie.setTitle("Filme numero"+j);
				movie.setYear(2013);
				movie.setActors(Arrays.asList(actor));
					
				Studio studio = new Studio();
				studio.setName("Studio de teste"+i);
			
				movie.setStudio(studio);
				
				movies.add(movie);
			}
			
			actor.setMovies(movies);
			
			actorDAO.createActor(actor);
		}

		System.out.println("Tempo para Criar Atores com Movies JDBC: ");
	}
}
